'''
Author: your name
Date: 2021-11-30 10:19:25
LastEditTime: 2021-12-01 16:57:06
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: \flood-evacuation-risk-assessment\SaftyAssessment.py
'''

import json
import numpy as np
#输入水深&流速，输出
def EscapeVelocity(hf, v):
    height = 1.7
    M = hf*v**2/9.81 + hf**2/2
    vesc = 0.6639*M**-0.103
    return vesc
    
#人体稳定性曲线
def ReadHumanStabilityCurve(fileName):
    with open(fileName) as f:
        data = json.load(f)
    depth = data['stability']['Adult']['ctDepth']
    velocity = data['stability']['Adult']['ctVelocity']
    return depth, velocity

#风险程度
def CalHazardDegree(hf, u, hcurve, ucurve):
    uInstability = np.interp(hf, hcurve, ucurve)
    if hf < 0.2:
        return 0
    HD = u/uInstability
    if HD > 1:
        HD = 1
    return HD


#计算不同位置的逃生时间
def CalEvaculationTime(depth, velocity, EscapePoint, EscapeLength, fileName):
    Results = np.empty([depth.shape[0], depth.shape[1]], dtype=float)
    #对于不同时间循环
    Hf, Uc = ReadHumanStabilityCurve(fileName)
    for i in range(0, depth.shape[0]):
        for j, Lines in enumerate(EscapePoint):
            tempTimeForaLine = []
            for k, item in enumerate(Lines):
                #获取一条逃生线段起始点的流态
                hs = depth.iloc[i+1, item[0]]
                he = depth.iloc[i+1, item[1]]
                us = velocity.iloc[i+1, item[0]]
                ue = velocity.iloc[i+1, item[0]]

                esVelocity = EscapeVelocity(0.5*(hs + he), 0.5*(us +ue))
                HD = CalHazardDegree(0.5*(hs + he), 0.5*(us +ue), Hf, Uc)
                if HD > 0.9 and 0.5*(hs + he) > 0.3:
                    esTime = 9999
                else:
                    esTime = EscapeLength[j][k] / esVelocity

                tempTimeForaLine.append(esTime)
            
            for k in range(0,len(Lines)):
                #Results[Lines[k][0]-1, i]  = sum(tempTimeForaLine[k+1:len(Lines)-1])
                Results[i, Lines[k][0]-1]  = sum(tempTimeForaLine[k:len(Lines)-1])
    return Results


    